//
//  PhotosListTableViewController.swift
//  Photos
//
//  Created by wangqi on 2021/10/20.
//

import UIKit

class PhotosListTableViewController: UITableViewController {
    var viewModel = PhotosListViewModel()
    override func viewDidLoad() {
        super.viewDidLoad()
        setupViews()
    }

}
private extension PhotosListTableViewController {
    func setupViews() -> () {
        setupNavigationBar()
        setupTableView()
    }
    func setupNavigationBar() -> () {
        title = "Photos List"
    }
    
    func setupTableView() -> () {
        tableView.rowHeight = 75
        tableView.setRefreshView {[weak self] in
            self?.refresh()
        } loadMoreBlock: {[weak self] in
            self?.loadMore()
        }
        tableView.refresh()
    }
}
private extension PhotosListTableViewController {
    func updateTableView() -> () {
        tableView.stopRefresh()
        tableView.footerView(hidden: !viewModel.haveNextPage)
        tableView.reloadData()
    }
}
private extension PhotosListTableViewController {
    func getCell() -> PhotoTableViewCell {
        let identifier = "PhotoTableViewCell"
        let result: PhotoTableViewCell = tableView.dequeueReusableCell(withIdentifier: identifier) { _ in
            let cell = PhotoTableViewCell(style: .default, reuseIdentifier: identifier)
            return cell
        }
        return result
    }
}
// MARK: - Table view data source
extension PhotosListTableViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.displayDatas.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let row = indexPath.row
        guard viewModel.displayDatas.count > row else {
            return UITableViewCell()
        }
        let item = viewModel.displayDatas[row]
        let cell = getCell()
        cell.photoImageView?.setImage(item.thumbnailUrl)
        cell.titleLabel?.text = item.title
        return cell
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let row = indexPath.row
        guard viewModel.displayDatas.count > row else {
            return
        }
        let item = viewModel.displayDatas[row]
        pushToPhotoDetail(item)
    }
}
private extension PhotosListTableViewController {
    func refresh() -> () {
        viewModel.refresh {[weak self] responseCode, _ in
            if responseCode == .no_error() {
                self?.updateTableView()
            }
        }
    }
    func loadMore() -> () {
        viewModel.loadMore {[weak self] responseCode, _ in
            if responseCode == .no_error() {
                self?.updateTableView()
            }
        }
    }
}
private extension PhotosListTableViewController {
    func pushToPhotoDetail(_ photo: Photo) -> () {
        guard let viewController = ViewControllerCenter.getPhotoDetailViewController() else { return }
        viewController.viewModel.detail = photo
        navigationController?.pushViewController(viewController, animated: true)
    }
}
